package com.ebs.md.fxmonitor.marketmonitor;

/**
 * Perforce Revision:
 *  $File: //depot/MarketData/fxmonitor/main/src/main/java/com/ebs/md/fxmonitor/marketmonitor/CommonTools.java $
 *  $Revision: #7 $  $Change: 116234 $  $DateTime: 2012/10/25 13:41:58 $  $Author: Arik.Sher $
 */

import java.math.BigDecimal;
import java.util.Collection;

import com.ebs.market.monitor.MarketMonitor.QuoteSide;
import com.ebs.md.api.BookSide;
import com.ebs.md.api.OrderSide;
import com.ebs.md.api.Rate;


public final class CommonTools
{
	private CommonTools() {}
	
	public static BigDecimal calcRatesDiff(Rate r1, Rate r2) {
		return r1.getValue().subtract(r2.getValue());
	}
	
	public static BigDecimal calcAbsRatesDiff(Rate r1, Rate r2) {
		return calcRatesDiff(r1,r2).abs();
	}
	
	/**
	 * 
	 * @param pipValue - or points. value for example: 6
	 * @param scale - (number of entries after decimal point, including trailing zeros)
	 *                for number 0.987 , the scale is 3
	 *                for number 0.1220, the scale is 4
	 *                for number 64,     the scale is 0
	 *                for number 2.3,    the scale is 1
	 * @return (pipValue / 10pow(scale) )
	 * 
	 * example : if pipValue is 15 and scale is 4 will return "0.0015"
	 */
	public static BigDecimal getRateByPip(int pipValue, int scale) {
		BigDecimal numB4Scale = new BigDecimal(pipValue);
		return numB4Scale.scaleByPowerOfTen( (-1) * scale );
	}

	/**
	 * @return true, if bd1 is greater than bd2
	 */
	public static boolean isGreater(BigDecimal bd1, BigDecimal bd2) {
		return (bd1.compareTo(bd2) > 0);
	}

	/**
	 * @return true, if bd1 is greater than bd2, or equals.
	 */
	public static boolean isGreaterEqual(BigDecimal bd1, BigDecimal bd2) {
		return (bd1.compareTo(bd2) >= 0);
	}
	
	public static QuoteSide getQuoteSide(int legacyCode) {
		return QuoteSide.valueOf(legacyCode);
	}
	
	public static QuoteSide getQuoteSide(OrderSide side) {
		return getQuoteSide(side.getLegacyCode());
	}
	
	public static QuoteSide getQuoteSide(BookSide side) {
		return getQuoteSide(side.getOrderSide());
	}
	
	public static <T> T safeExtractedFirst(Collection<T> coll) {
		if ( coll == null || coll.isEmpty() ) {
			return null;
		}
		return coll.iterator().next();
	}

}
